#include"lottery_sys.h"
void open_lott(Buyer_t* user_head,Lottery_t* lott_head)
{
	if(user_head -> next == NULL)
		return;
	int grand = 0;
	L_buyed_t* buyed_head = NULL;
	Lottery_t* tmp_lott = NULL;
	L_buyed_t* tmp_buy = NULL;
	Buyer_t* tmp_user = NULL;
	buyed_head = load_buyed_lott();	//加载已购买彩票链表
	tmp_buy = buyed_head -> next;
	tmp_lott = lott_head -> next;
	printf("奖池金额：%f\n",tmp_lott -> money); 
	tmp_user = user_head -> next;
	if(!tmp_buy)
	{
		printf("还没人彩票！\n");
		return;
	}
	if(!tmp_lott)
	{
		printf("还没开奖呢！\n");
		return;
	}
	if(!tmp_user)
	{
		printf("还没人买票！\n");
		return;
	}
	int i = 0;
	int win_nums[8]; 
	if(tmp_lott -> L_yn_open == 0)
	{
		printf("奖池金额：%f\n",tmp_lott -> money);
		printf("开奖号码：");
		srand((unsigned) time(NULL));
		for (; i < 5; i++)
		{   
			win_nums[i] = rand() % 5; 
			printf("%d ", win_nums[i]);
		}   
		printf("\n");
		while(tmp_buy != NULL )
		{   
			if(tmp_buy -> date == tmp_lott -> date)
			{
				grand = checkwinner(tmp_buy -> chose_nums, win_nums);
				printf("彩票ID ：%ld\n",tmp_buy -> ID);
				i = 0;
				while(i < 5)
					printf("%d ",tmp_buy -> chose_nums[i++]);
				i = 0;
				printf("\n");
				if(grand > 1)
				{
					while(tmp_user != NULL)
					{
						if(!strcmp(tmp_buy -> buyer, tmp_user -> B_name))
						{
							tmp_user -> B_remain_money += grand*(tmp_lott -> money)/100;
							tmp_lott -> money -= grand*(tmp_lott -> money)/100;
							printf("grand=%d\n",grand);
							printf("奖池金额：%f\n",tmp_lott -> money);
							printf("中奖了!");
							printf("最新余额：%f\n",tmp_user -> B_remain_money);
							printf("奖池余额：%f\n",tmp_lott -> money);
						}
						tmp_user = tmp_user -> next;
					}
				}
			}   
			tmp_buy = tmp_buy -> next;
		}
		tmp_lott -> L_yn_open = 1;
		printf("按回车键返回！\n");
		cleaninput();
	}
	else
	{
		printf("已开奖！\n");
		return;
	}
	save_buylott_list(buyed_head);
	free_buylott_list(buyed_head);
}

